﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WarningForeverClone.src.ships;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WarningForeverClone.src.weapons
{
    public class Rocket : Weapon
    {
        const int damage = 10;
        const string textureName = "Textures/Weapons/rocket";

        static Texture2D texture;
        static Color[] textureBits;

        Ship target;
        public const int speed = 1;

        double angle; //angle for velocity
        bool firstUpdate = true;
        float velocityIncrease;

        public Rocket()
             : base(Vector2.Zero)
        {
            velocityIncrease = 1;
            if (texture == null)
            {
                if (MainGame.ContentManager != null)
                {
                    texture = MainGame.ContentManager.Load<Texture2D>(textureName);
                    textureBits = new Color[texture.Width * texture.Height];
                    texture.GetData<Color>(textureBits);
                }
            }
        }

        public override void Reset()
        {
            base.Reset();
            Alive = true;
        }

        public override void Update()
        {
            base.Update();
            double rot = Math.Atan2((target.Position.Y + target.Height/2) - Position.Y, (target.Position.X + target.Width / 2) - Position.X);
            if (firstUpdate)
            {
                angle = rot;
                firstUpdate = false;
            }

            Vector2 md = Velocity; //missile heading
            Vector2 td = target.Position - Position;
            float sign = -md.Y * td.X + md.X * td.Y; // cross and dot at the same time 

            if(sign > 0)
                angle += 0.025f;
            else
                angle -= 0.025f;

            //double half = angle + ((rot - angle) / 100);
            //angle = rot;

            Velocity = new Vector2((float)(speed * Math.Cos(angle)), (float)(speed * Math.Sin(angle)));
            //float dot = Vector2.Dot(md, td);
            //double angleBetween = Math.Acos(dot / (md.Length() * td.Length())) * (180/Math.PI);

            //if (angleBetween < 45)
            //{
            //    velocityIncrease = (float)(2 * (45 - angleBetween) / 45);
            //}

            Rotation = Math.PI / 2 + rot;
            velocityIncrease += 0.05f;
        }


        public Ship Target
        {
            get { return target; }
            set { target = value; }
        }

        public override Vector2 Velocity
        {
            get
            {
                return base.Velocity * (1+velocityIncrease);
            }
        }

        public override float Damage
        {
            get
            {
                return damage;
            }
        }
        public override Texture2D Texture
        {
            get { return texture; }
        }
        public override Color[] TextureData
        {
            get { return textureBits; }
        }
        public static int Width
        {
            get { return texture.Width; }
        }

        public override Microsoft.Xna.Framework.Vector2 Acceleration
        {
            get { return new Vector2(0, 0); }
        }
    }
}
